<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="findMsg"
        placeholder=" 请输入关键词"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索 / 未处理
      </el-button>

      <el-button v-waves class="filter-item" type="success" @click="getPassList">
        已通过
      </el-button>

      <el-button v-waves class="filter-item" type="danger" @click="getUnPassList">
        已拒绝
      </el-button>
    </div>
    <el-table

      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      tablder
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--      @sort-change="sortChange"-->
      <!--      <el-table-column-->
      <!--        label="序号"-->
      <!--        prop="udOdId"-->
      <!--        sortable="custom"-->
      <!--        align="center"-->
      <!--        width="80"-->
      <!--        :class-name="getSortClass('id')"-->
      <!--      />-->
      <el-table-column prop="index" label="序号" align="center" width="80" fixed>
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>

      <el-table-column label="认证信息" prop="authMsg" align="center" />

      <el-table-column label="认证描述" prop="authText" align="center" />

      <!--      操作-->
      <el-table-column label="认证图片" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">
          <el-button v-if="row.status!='deleted'" size="mini" type="primary" @click="findImg(row.row.authImg)">
            查看图片
          </el-button>
        </template>
      </el-table-column>

      <!--      操作-->
      <el-table-column label="用户详细" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">
          <el-button v-if="row.status!='deleted'" size="mini" type="primary" @click="findUser(row.row.authUserId)">
            用户详细
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="要认证的身份" prop="authUserStatus" align="center">
        <template slot-scope="scope">

          <span>{{ scope.row.authUserStatus == "1" ? "设计师" : (scope.row.authUserStatus == "2" ? "品牌商" : "") }}</span>
        </template>

      </el-table-column>

      <!--      操作-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="row">

          <el-button v-if="flag==1?false:true" size="mini" type="success" @click="pass(row.row)">
            通过
          </el-button>
          <el-button v-if="flag==2?false:true" size="mini" type="danger" @click="unpass(row.row.authId)">
            拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!--    查看上传图片-->

    <template slot-scope="scope">
      <el-popover placement="top-start" trigger="click"> <!--trigger属性值：hover、click、focus 和 manual-->
        <a :href="scope.row.goodsImg" target="_blank" title="查看最大化图片">
          <img :src="scope.row.goodsImg" style="width: 300px;height: 300px">
        </a>
        <img slot="reference" :src="scope.row.goodsImg" style="width: 50px;height: 50px; cursor:pointer">
      </el-popover>
    </template>

    <el-dialog
      title="审核资料"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <!--弹窗内容-->
      <!--      <el-descriptions :column="1" border>-->

      <!--        <el-descriptions-item v-for="item in imgList" label="审核资料">-->
      <!--          <viewer>-->
      <!--          <el-image-->
      <!--            style="width: 150px; height: 150px; border:none;cursor: pointer;"-->
      <!--            :src="getImage(item.imagename)"-->
      <!--          >’-->
      <!--          </el-image>-->
      <!--          </viewer>-->
      <!--        </el-descriptions-item>-->

      <!--      </el-descriptions>-->

      <div>
        <viewer :images="imgList">
          <img
            v-for="(src,index) in imgList"
            :key="index"
            style="width: 196px; height: 196px; border:none;cursor: pointer;"
            :src="getImage(src.imagename)"
          >
        </viewer>
      </div>

    </el-dialog>

    <!--    查看用户信息-->
    <el-dialog
      title="用户信息"
      :visible.sync="dialogVisible2"
      width="30%"
    >
      <!--弹窗内容-->
      <el-descriptions :column="1" border>
        <el-descriptions-item label="微信头像">      <!--        图片-->
          <el-image
            style="width: 100px; height: 100px; border:none;cursor: pointer;"
            :src="getWxIconImage(this.usermsg.userIcon)"
          >’
          </el-image>
        </el-descriptions-item>
        <el-descriptions-item label="用户名">{{ usermsg.userName }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ usermsg.userSex == 0 ? "男" : "女" }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ usermsg.userPhone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ usermsg.userEmail }}</el-descriptions-item>

      </el-descriptions>

    </el-dialog>

  </div>
</template>

<script>

import waves from '@/directive/waves' // waves directive

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

import { getAuthList, updatestate, getPassList, getUnpassList } from '@/api/auth'

import { getuserByid, updateAuth } from '@/api/userAndDesign'

import { authBrand } from '@/api/brand'

const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
// eslint-disable-next-line no-unused-vars
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  // eslint-disable-next-line vue/no-unused-components
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      // 刷新标识符
      flag: 0,
      // 表格列表
      list: [],
      // 搜索框
      findMsg: '',
      // 保险弹出
      dialogVisible: false,
      // 查看用户信息
      dialogVisible2: false,
      // 查看图片列表
      imgList: [],

      usermsg: {},
      // 分页总数
      total: 0,
      // 当前页
      currentPage: 1,
      // 每页多少条
      pagesizes: 5,

      // 搜索下拉列表
      // importanceOptions: [1, 2, 3],
      tableKey: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      // 获取列表
      getAuthList(this.findMsg, this.currentPage, this.pagesizes).then(res => {
        this.list = res.data.records

        console.log('这是getList')

        console.log(res)

        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 已通过列表
    getPassList() {
      this.flag = 1
      getPassList(this.findMsg, this.currentPage, this.pagesizes).then(res => {
        this.list = res.data.records

        console.log('这是getpassList')

        console.log(res)

        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 已拒绝列表
    getUnPassList() {
      this.flag = 2
      getUnpassList(this.findMsg, this.currentPage, this.pagesizes).then(res => {
        this.list = res.data.records

        console.log('这是getunpassList')

        console.log(res)

        this.total = res.data.total
        this.listLoading = false
      })
    },
    findImg(id) {
      this.imgList = []
      console.log('这是查看图片！')
      var obj = JSON.parse(id) // JSON.parse() 把 json字符串 转化成 json 对象

      for (const i in obj) {
        this.imgList.push(obj[i])
      }

      console.log(this.imgList)

      this.dialogVisible = true
    },

    findUser(userId) {
      console.log('这是查看用户！')
      console.log(userId)

      getuserByid(userId).then(res => {
        console.log(res)
        this.usermsg = res.data
      })

      this.dialogVisible2 = true
    },
    // 通过
    pass(row) {
      console.log('这是pass')
      this.$confirm('此操作将通过该用户的认证, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row)
        // const userAuth = {
        //   userId: row.authUserId,
        //   userText: row.authText,
        //   userrCertify: row.authImg,
        //   userDesignerStyle: row.authMsg
        // }
        // 如果是 1 则认证设计师 如果是 2则认证品牌商
        if (row.authUserStatus == '1') {
          const userAuth = {
            userId: row.authUserId,
            userText: row.authText,
            userrCertify: row.authImg,
            userDesignerStyle: row.authMsg
          }
          updateAuth(userAuth).then(res => {
            console.log(res)
            if (res.code = 20000) {
              updatestate(row.authId, 1).then(res => {
                console.log(res)

                // 修改完成 更新认证表改为已经认证
                if (res.code == 20000) {
                  if (this.flag == 0) {
                    this.getList()
                  } else if (this.flag == 1) {
                    this.getPassList()
                  } else {
                    this.getUnPassList()
                  }
                } else {
                  this.$notify.error({
                    title: '错误',
                    message: '服务器繁忙！'
                  })
                }
              })
              this.$notify({
                title: '成功',
                message: res.data,
                type: 'success'
              })
            }
          })
        } else if (row.authUserStatus == '2') {
          const userBrand = {
            userId: row.authUserId,
            brandsName: row.authMsg,
            brandsText: row.authText,
            brandsCertify: row.authImg
          }
          console.log('品牌商认证')
          console.log(userBrand)

          authBrand(userBrand).then(res => {
            console.log(res)

            // 修改完成 更新认证表改为已经认证
            if (res.code = 20000) {
              updatestate(row.authId, 1).then(res => {
                console.log(res)
                if (res.code == 20000) {
                  if (this.flag == 0) {
                    this.getList()
                  } else if (this.flag == 1) {
                    this.getPassList()
                  } else {
                    this.getUnPassList()
                  }
                } else {
                  this.$notify.error({
                    title: '错误',
                    message: '服务器繁忙！'
                  })
                }
              })
              this.$notify({
                title: '成功',
                message: res.data,
                type: 'success'
              })
            }
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })
    },
    // 拒绝
    unpass(authId) {
      this.$confirm('此操作将拒绝通过该用户认证, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updatestate(authId, 2).then(res => {
          console.log(res)
          if (res.code == 20000) {
            this.$notify({
              title: '成功',
              message: res.data,
              type: 'success'
            })
            if (this.flag == 0) {
              this.getList()
            } else if (this.flag == 1) {
              this.getPassList()
            } else {
              this.getUnPassList()
            }
          } else {
            this.$notify.error({
              title: '错误',
              message: '服务器繁忙！'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        })
      })

      console.log('这是pass')
      console.log(authId)
    },
    // 搜索框
    handleFilter() {
      this.flag = 0
      this.getList()
    },
    // 分页
    handleSizeChange(val) {
      this.pagesizes = val
      console.log(this.pagesizes)
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(this.currentPage)
      this.getList()
    },
    // 得到图片
    getImage(image) {
      // return `http://localhost:9527/dev-api/common/download?name=${image}`
      return `http://localhost:9527/dev-api/common/download?name=${image}`
    },
    getWxIconImage(url) {
      // console.log(url)
      return `${url}`
    },

    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
